package com.kischang.blog.web.action;

import com.kischang.blog.model.*;
import com.kischang.blog.service.TitleMenuService;
import com.kischang.blog.service.TopicService;
import com.kischang.blog.web.actionuser.LoginAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
@Scope("prototype")
public class TopicAction extends ActionSupport implements ModelDriven<PageInfo> {


    private static final long serialVersionUID = 4596453037765731311L;

    @Autowired
    private TitleMenuService titleMenuService;
    @Autowired
    private TopicService topicService;

    private List<TitleMenu> menulist;
    private List<Reply> replylist;
    private List<Topic> recommenderList;
    private Topic topic;
    private int mid;
    private int tid;

    private PageInfo pageInfo = new PageInfo(10);

    @Override
    public String execute() throws Exception {
        User user = (User) ActionContext.getContext().getSession().get(LoginAction.SESSION_USER);
        menulist = titleMenuService.getAllParentMenu();
        topic = topicService.getTopicByID(tid, true, user);
        replylist = topicService.getReviewByPageAndTopicID(topic.getId(), pageInfo);
        recommenderList = topicService.getRecommend(tid);
        pageInfo.setUrl("?tid=" + tid + "&mid=" + mid);
        return SUCCESS;
    }

    public List<TitleMenu> getMenulist() {
        return menulist;
    }

    public int getMid() {
        return mid;
    }

    public void setMid(int mid) {
        this.mid = mid;
    }

    public void setTid(int tid) {
        this.tid = tid;
    }

    @Override
    public PageInfo getModel() {
        return pageInfo;
    }

    public Topic getTopic() {
        return topic;
    }

    public List<Reply> getReplylist() {
        return replylist;
    }

    public List<Topic> getRecommenderList() {
        return recommenderList;
    }
}
